<template>
	<view>
		<view class="list b-b">
			<text class="title">收货人</text>
			<input class="input" type="text" v-model="addressInfo.trueName" placeholder="收货人姓名"
				placeholder-class="placeholder" />
		</view>
		<view class="list b-b">
			<text class="title">手机号</text>
			<input class="input" type="text" v-model="addressInfo.mobileNum" placeholder="收货人手机号"
				placeholder-class="placeholder" />
		</view>
		<view class="list b-b">
			<text class="title">省市区</text>
			<text @click="chooseLocation" class="input">
				{{addressInfo.areaName ? addressInfo.areaName : '在地图中选择'}}
			</text>
			<text class="yticon icon-shouhuodizhi" @click="chooseLocation"></text>
		</view>
		<view class="list b-b">
			<text class="title">详细地址</text>
			<input class="input" type="text" v-model="addressInfo.areaInfo" placeholder="街道/小区/门牌号"
				placeholder-class="placeholder" />
		</view>

		<view class="list default">
			<text class="title">设为默认</text>
			<switch :checked="addressInfo.defaultVal == 1" color="#fa436a" @change="switchChanged" />
		</view>

		<button class="submit" @click="sumbit">提交</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				addressInfo: {
					addrId: '',
					areaId: '',
					trueName: '',
					mobileNum: '',
					addressInfo: '',
					areaName: '',
					defaultVal: 0
				}
			}
		},
		onLoad(option) {
			let title = '新增收货地址';
			if(option.type==='edit'){
				title = '编辑收货地址'
	
				this.addressInfo = JSON.parse(option.data)
			}

			uni.setNavigationBarTitle({
				title
			})
		},
		methods: {
			chooseLocation() {
				uni.chooseLocation({
					success: (data) => {
						this.addressInfo.areaName = data.address;
					}
				})
			},
			async sumbit() {
				let address = this.addressInfo;
				if (!address.trueName) {
					this.$api.msg('请填写收货人姓名');
					return;
				}
				if (!/(^1[3|4|5|7|8][0-9]{9}$)/.test(address.mobileNum)) {
					this.$api.msg('请输入正确的手机号码');
					return;
				}
				if (!address.areaName) {
					this.$api.msg('请在地图选择所在位置');
					return;
				}
				if (!address.areaInfo) {
					this.$api.msg('请填写详细地址');
					return;
				}

				let data = {
					"areaInfo": this.addressInfo.areaInfo,
					"mobile": this.addressInfo.mobileNum,
					"areaId": "4523256",
					"trueName": this.addressInfo.trueName,
					"defaultVal": this.addressInfo.defaultVal,
					"addrId": this.addressInfo.addrId
				}
				
				let res = await this.helper.requestAPI(this.api.step2,data);
				this.$api.msg('提交成功');
				this.$api.prePage().refreshList();
				setTimeout(() => {
					uni.navigateBack()
				}, 800)
			},
			switchChanged(e) {
				console.log(e);
				this.addressInfo.default = e.target.value;
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
		padding-top: 16rpx;
	}

	.list {
		display: flex;
		align-items: center;
		padding: 0 30rpx;
		height: 110rpx;
		background: #fff;
		position: relative;

		.title {
			flex-shrink: 0;
			width: 130rpx;
			font-size: $uni-font-size-base;
		}

		.placeholder {
			font-size: $uni-font-size-base;
		}

		.input {
			font-size: $uni-font-size-base;
			flex: 1;
		}
	}

	.default {
		margin-top: 16rpx;

		.title {
			flex: 1;
		}

		switch {
			transform: translateX(16rpx) scale(.8);
		}
	}

	.submit {
		display: flex;
		justify-content: center;
		align-items: center;
		color: #fff;
		width: 90%;
		height: 80rpx;
		margin: 60rpx auto;
		background-color: $base-color;
		border-radius: 10rpx;
		font-size: $uni-font-size-base;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
</style>
